package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.RepairLine;
import com.zyd.shiro.business.service.PakRepairLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakRepairLine;
import com.zyd.shiro.persistence.mapper.PakRepairLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakRepairLineServiceImpl implements PakRepairLineService {

    @Autowired
    private PakRepairLineMapper pakRepairLineMapper;

    @Override
    public RepairLine insert(RepairLine repairLine) {
        Assert.notNull(repairLine, "对象不可为空！");
        repairLine.getPakRepairLine().setCreateTime(new Date());
        repairLine.getPakRepairLine().setUpdateTime(new Date());
        pakRepairLineMapper.insertSelective(repairLine.getPakRepairLine());
        return repairLine;
    }

    @Override
    public void insertList(List<RepairLine> repairLines) {
            for(RepairLine repairLine:repairLines){
                pakRepairLineMapper.insertSelective(repairLine.getPakRepairLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakRepairLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(RepairLine repairLine) {
        Assert.notNull(repairLine, "对象不可为空！");
        repairLine.setUpdateTime(new Date());
        return pakRepairLineMapper.updateByPrimaryKey(repairLine.getPakRepairLine()) > 0;
    }

    @Override
    public boolean updateSelective(RepairLine repairLine) {
        Assert.notNull(repairLine, "对象不可为空！");
        repairLine.setUpdateTime(new Date());
        return pakRepairLineMapper.updateByPrimaryKeySelective(repairLine.getPakRepairLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public RepairLine getByPrimaryKey(Long primaryKey) {
        PakRepairLine pakRepairLine = pakRepairLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakRepairLine ? null : new RepairLine(pakRepairLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public RepairLine getOneByEntity(RepairLine repairLine) {
        Assert.notNull(repairLine, "对象不可为空！");
        PakRepairLine pakRepairLine = pakRepairLineMapper.selectOne(repairLine.getPakRepairLine());
        return null == pakRepairLine ? null : new RepairLine(pakRepairLine);
    }

    @Override
    public List<RepairLine> listAll() {
        List<PakRepairLine> pakRepairLines = pakRepairLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakRepairLines)) {
            return null;
        }
        List<RepairLine> repairLines = new ArrayList<>();
        for (PakRepairLine repairLine : pakRepairLines) {
            repairLines.add(new RepairLine(repairLine));
        }
        return repairLines;
    }

    @Override
    public List<RepairLine> listByEntity(RepairLine repairLine) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<RepairLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakRepairLine> pakRepairLines = pakRepairLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakRepairLines)) {
            return null;
        }
        List<RepairLine> repairLines = new ArrayList<>();
        for (PakRepairLine pakRepairLine : pakRepairLines) {
            repairLines.add(new RepairLine(pakRepairLine));
        }
        PageInfo bean = new PageInfo<PakRepairLine>(pakRepairLines);
        bean.setList(repairLines);
        return bean;
    }
@Override
    public void increase(PakRepairLine pakRepairLine) {
    pakRepairLineMapper.insert(pakRepairLine);
    }

    @Override
    public List<PakRepairLine> findByRepair(Long repairId) {
        return pakRepairLineMapper.findByRepair(repairId);
    }


}
